﻿/*
 * File: TwoDeeGamePlayBaseScene_Updaate.cs
 * Author: Gourav Das
 * Purpose: Update for gameplay
 * Created: May30 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    partial class GamePlayBaseScene : Scene
    {
        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you switch to a different application.
        /// </summary>
        public override void Update(GameTime _gameTime, bool _otherSceneHasFocus,
                                                       bool _coveredByOtherScene)
        {
            base.Update(_gameTime, _otherSceneHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (_coveredByOtherScene)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                if (KBRedBall != null)
                {
                    if (KBRedBall.Position.Y < -15)
                    {
                        broadcastBallChaseCam.ResetCamera();
                        birdEyeViewChaseCamera.ResetCamera();
                    }
                    else
                    {
                        broadcastBallChaseCam.Update(_gameTime, KBRedBall.Position);
                        birdEyeViewChaseCamera.Update(_gameTime, KBRedBall.Position);
                    }
                }

                if (!PausePhysics)
                    PhysxScene.Simulate(_gameTime.ElapsedGameTime.TotalSeconds);

                if (bMobileDissappear || bMobileAppear)
                    TransitionValue += (float)_gameTime.ElapsedGameTime.Milliseconds / 15f;

                AnimateInfoBarSlidingEffect();

                if (DblClickTimeBomb == true)
                {
                    DblClickElapsedTimer += _gameTime.ElapsedGameTime;
                }
                else
                {
                    DblClickElapsedTimer = TimeSpan.Zero;
                    //click1 = false;
                }

                photoFrames[0].Update();
                photoFrames[1].Update();
                photoFrames[2].Update();
                photoFrames[3].Update();

                rightSideBarModel.Update();
                Team1Logo.Update();
                Team2Logo.Update();

                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && FirstColCurrentXPos < FirstColFinalXPos)
                {
                    FirstColCurrentXPos += 2;
                    if (FirstColCurrentXPos > FirstColFinalXPos)
                        FirstColCurrentXPos = FirstColFinalXPos;
                }
                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && SecondColCurrentXPos < SecondColFinalXPos)
                {
                    SecondColCurrentXPos += 2;
                    if (SecondColCurrentXPos > SecondColFinalXPos)
                        SecondColCurrentXPos = SecondColFinalXPos;
                }
                if ((DisplayRemainingBatsMan || DisplayRemainingBowler) && ThirdColCurrentXPos < ThirdColFinalXPos)
                {
                    ThirdColCurrentXPos += 2;
                    if (ThirdColCurrentXPos > ThirdColFinalXPos)
                        ThirdColCurrentXPos = ThirdColFinalXPos;
                }

                //Change Infobar message every N secods
                elapsedTime += _gameTime.ElapsedGameTime;
                if (elapsedTime > TimeSpan.FromSeconds(INFOBARMESSAGEINTERVAL))
                {
                    idx++;
                    elapsedTime -= TimeSpan.FromSeconds(INFOBARMESSAGEINTERVAL);
                    sInfoBarMessage = Game22Yards.DynMsgStore.Messages[idx % Game22Yards.DynMsgStore.Size].Msg;
                }

                if (PhotoFrameAnimationTimeBomb)
                {
                    PhotoFramesAnimationElapsedTime += _gameTime.ElapsedGameTime;
                    if (PhotoFramesAnimationElapsedTime > TimeSpan.FromSeconds(1.2f))
                    {
                        if (PhotoFramesAnimationElapsedTime > TimeSpan.FromSeconds(1.7f))
                        {
                            PhotoFramesAnimationElapsedTime = TimeSpan.Zero;
                            if ((photoFrames[0].IsMouseLeftClickSelected || photoFrames[1].IsMouseLeftClickSelected) && RendereredGroupToBatsmen)
                                DisplayRemainingBatsMan = true;
                            if ((photoFrames[2].IsMouseLeftClickSelected || photoFrames[3].IsMouseLeftClickSelected) && RendereredGroupToBatsmen)
                                DisplayRemainingBowler = true;
                            PhotoFrameAnimationTimeBomb = false;
                        }
                    }
                }

                if (TransitionTimeBomb)
                {
                    TransitionelapsedTime += _gameTime.ElapsedGameTime;
                    if (TransitionelapsedTime > TimeSpan.FromSeconds(1.2f))
                    {
                        if (!TextPrintedOnTexture)
                        {
                            if (SubScreenState == (short)TwoDEESubScreenState.Summary)
                            {
                                TextureSummaryScreen = TextureScreen;
                                Draw2DSceneSummary(ref TextureSummaryScreen);
                                TextureMaterial tm = new TextureMaterial();
                                tm.ColorMapTexture = TextureSummaryScreen;
                                tm.NormalMapTexture = TextureScreenNormal;
                                tm.SpecularMapTexture = TextureScreenSpec;
                                rightSideBarModel.SetModelMeshTextureMaterial("screen", tm);
                                TextPrintedOnTexture = true;
                            }
                            if (SubScreenState == (short)TwoDEESubScreenState.ScoreCardView)
                            {
                                TextureScoreCardScreen = TextureScreen;
                                Draw2DSceneScoreCardView(ref TextureScoreCardScreen);
                                TextureMaterial tm = new TextureMaterial();
                                tm.ColorMapTexture = TextureScoreCardScreen;
                                tm.NormalMapTexture = TextureScreenNormal;
                                tm.SpecularMapTexture = TextureScreenSpec;
                                rightSideBarModel.SetModelMeshTextureMaterial("screen", tm);
                                TextPrintedOnTexture = true;
                            }
                        }
                        if (TransitionelapsedTime > TimeSpan.FromSeconds(2.2f))
                        {
                            TransitionelapsedTime -= TimeSpan.FromSeconds(2.2f);
                            rightSideBarModel.transitionIn = true;
                            rightSideBarModel.Oscillate = true;
                            TransitionTimeBomb = false;
                        }
                    }
                }
            }
        }
    }
}
